const { createApp, createVNode, render, ref, h, inject } = Vue


const options = { 
    moduleCache: {
        vue: Vue,
    }, 
    getFile(url) {
        return fetch(url).then(response => response.ok ? response.text() : Promise.reject(response));
    }, 
    addStyle(styleStr) {
        const style = document.createElement('style');
        style.textContent = styleStr;
        const ref = document.head.getElementsByTagName('style')[0] || null;
        document.head.insertBefore(style, ref);
    }, 
    log(type, ...args) {
        console.log(type, ...args);
    }
}
const { loadModule, version } = window["vue3-sfc-loader"];
const FooterComponent = Vue.defineAsyncComponent(() => loadModule('pages/footer.vue', options));
const MainComponent = Vue.defineAsyncComponent(() => loadModule('pages/main.vue', options));
const MessageComponent = await loadModule('components/message.vue', options);
const ConfirmComponent = await loadModule('components/confirm.vue', options);

const app = Vue.createApp({
    components: {
        'copyrighter': FooterComponent,
        'mainpage': MainComponent
    },
});
app.mount('#app'); 

const showMessage = (message, type) => { 
    const messageInstance = h(MessageComponent, { message, type });  
    const container = document.createElement("div")
    document.body.appendChild(container);
    render(messageInstance, container);
    setTimeout(() => {
        render(null, container);
        document.body.removeChild(container)
    }, 2000);
};
window.message = showMessage

const showConfirm = (content,cancelBtn,successBtn) => {
    let showCancel = true;
    const confirmInstance = h(ConfirmComponent, { content,showCancel,successBtn,cancelBtn });
    const container = document.createElement("div")
    document.body.appendChild(container);
    render(confirmInstance, container);
    confirmInstance.component.exposed.show();
    // 注册事件监听器
    confirmInstance.el.addEventListener('close', (data) => {
        render(null, container);
        document.body.removeChild(container);
    });

};
window.confirm = showConfirm
